/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data.ActivityLog;

import CrlException.CrlException;
import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @author CUHUUHOANG
 */
public class ActivityData {

    static Queue<String> ActivityLogQueue;
    public static boolean isUsing;

    public ActivityData() throws CrlException {
        try {
            ActivityLogQueue = new LinkedList<String>();
            isUsing = false;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }
    }

    synchronized public void addAtivity(String log) {
        try {
            if (isUsing) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new CrlException(e.getMessage());
                }
            }
            isUsing = true;
            //do job
            if(log!=null)
            ActivityLogQueue.add(log);
            //end job
            isUsing = false;
            notifyAll();
        } catch (Exception e) {
            //do nothing
        }
    }

    synchronized public String getAtivity() throws CrlException {
        try {
            if (isUsing) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new CrlException(e.getMessage());
                }
            }
            isUsing = true;
            //do job
            String log = ActivityLogQueue.poll();
            //end job
            isUsing = false;
            notifyAll();
            return log;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }
    }

    synchronized public int getSize() throws CrlException {
        try {
            if (isUsing) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new CrlException(e.getMessage());
                }
            }
            isUsing = true;
            //do job
            int out = ActivityLogQueue.size();
            //end job
            isUsing = false;
            notifyAll();
            return out;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }
    }
}
